package priv.dajie.tzzq.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import priv.dajie.tzzq.dao.BuffMapper;
import priv.dajie.tzzq.model.QualityName;
import priv.dajie.tzzq.service.PlatformService;
import priv.dajie.tzzq.spider.model.ItemInfo;
import priv.dajie.tzzq.spider.model.SellingBuyInfo;
import priv.dajie.tzzq.spider.model.SellingInfo;
import priv.dajie.tzzq.spider.model.TradeType;

/**
 * @ClassName BuffServiceImp
 * @Description
 * @Author dajie
 * @Date 2019/10/23 11:53 下午
 * @Version V1.0
 **/
public class BuffServiceImpl implements PlatformService {

    @Autowired
    BuffMapper buffMapper;

    public boolean addItemFullPro(SellingBuyInfo sellingBuyInfo){
        boolean state = buffMapper.updateItemFullPro(sellingBuyInfo);
        if (!state){
            state = buffMapper.addItemFullPro(sellingBuyInfo);
        }
        return state;
    }

    public boolean addItemFull(SellingBuyInfo sellingBuyInfo){
        buffMapper.addItem(new SellingInfo(sellingBuyInfo.getInfo(), sellingBuyInfo.getBuyPrice(), sellingBuyInfo.getBuyNum(), TradeType.BUY));
        buffMapper.addItem(new SellingInfo(sellingBuyInfo.getInfo(), sellingBuyInfo.getSellPrice(), sellingBuyInfo.getSellNum(), TradeType.SELL));
        return true;
    }

    @Override
    public boolean addItemPro(SellingInfo sellingInfo){
        // 如果更新失败则表示数据库不在该数据，则更新
        boolean state = buffMapper.updateItemPro(sellingInfo);
        if (!state){
            state = buffMapper.addItemPro(sellingInfo);
        }
        return state;
    }

    @Override
    public boolean addItem(SellingInfo sellingInfo){

        boolean state = buffMapper.updateItemPro(sellingInfo);
        return state;
    }

    @Override
    public boolean addInfo(ItemInfo itemInfo){
        boolean state = buffMapper.updateInfo(itemInfo);
        if(!state) {
         state=buffMapper.addInfo(itemInfo);
        }
        return state;
    }

    @Override
    public boolean updateInfo(ItemInfo itemInfo){
        boolean state = buffMapper.updateInfo(itemInfo);
        return state;
    }

    @Override
    public Integer countItem(String buffId){
        Integer itemCount = buffMapper.getItemCount(buffId);
        return itemCount;
    }

    @Override
    public ItemInfo getInfo(ItemInfo itemInfo){
        ItemInfo info = buffMapper.getInfo(itemInfo);
        return info;
    }

    @Override
    public ItemInfo getInfoBySteamName(String steamName){
        ItemInfo itemInfo = new ItemInfo();
        itemInfo.setSteamName(steamName);
        return getInfo(itemInfo);
    }

    @Override
    public ItemInfo getInfoById(String buffId){
        ItemInfo itemInfo = new ItemInfo();
        itemInfo.setId(buffId);
        return getInfo(itemInfo);
    }

    @Override
    public ItemInfo getInfoByQualityName(QualityName qualityName){
        ItemInfo itemInfo = new ItemInfo();
        itemInfo.setQualityName(qualityName);
        return getInfo(itemInfo);
    }

}
